package com.hx.springbootmybstis.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springbootmybstis.mapper.UserMapper;
import com.hx.springbootmybstis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author: 若若叶
 * @date: 2021/9/29 16:44 FileName: UserController Description:
 */
@Controller
public class UserController {
  @Autowired private UserMapper userMapper;

  @RequestMapping("/login")
  public String toLoginPage() {
    return "pages/login";
  }

  @RequestMapping("/t1")
  public String t1(Model model) {
    User user = new User(6, "lisi", 20);
    model.addAttribute("user", user);
    Date date = new Date();
    model.addAttribute("time", date);
    model.addAttribute("sex", 0);
    model.addAttribute("count", 1);
    return "test01";
  }

  @RequestMapping("/t2")
  public String t2(Model model) {
    List<User> list = new ArrayList<>();
    User user1 = new User(7, "aa", 20);
    User user2 = new User(8, "bb", 20);
    User user3 = new User(9, "cc", 20);
    User user4 = new User(10, "dd", 20);
    list.add(user1);
    list.add(user2);
    list.add(user3);
    list.add(user4);
    model.addAttribute("userList", list);
    return "test02";
  }

  @RequestMapping("/t3")
  public String t3(Model model) {
    Map<String, User> map = new HashMap<>();
    map.put("user1", new User(7, "aa", 20));
    map.put("user2", new User(8, "bb", 20));
    map.put("user3", new User(9, "cc", 20));
    model.addAttribute("userMap", map);
    return "test03";
  }

  @RequestMapping("/t4")
  public String t4(Model model) {
    User user1 = new User(7, "aa", 20);
    model.addAttribute("u", user1);
    return "test04";
  }

  @RequestMapping("/t5")
  public String t5(HttpServletRequest request) {
    request.setAttribute("u", "hx");
    // 获取session
    HttpSession session = request.getSession();
    // 赋值
    session.setAttribute("username", "lisi");
    ServletContext servletContext = request.getServletContext();
    servletContext.setAttribute("info", "登录失败");
    return "test05";
  }

  @RequestMapping("/findAllUserInfo")
  public String findAllUserInfo() {
    System.out.printf("/findAllUserInfo");
    return "test05";
  }

  @RequestMapping("findUserById")
  public String findUserById(Integer id, String name) {
    System.out.printf("id:" + id);
    System.out.printf("name:" + name);
    return "test05";
  }

  /**
   * @param current 当前页
   * @param pageSize 每页显示多少条
   * @return
   */
  @RequestMapping("/findUserInfoByPage")
  public String findUserInfoByPage(Model model, Integer current, Integer pageSize) {
    PageHelper.startPage(current, pageSize);
    List<User> list = userMapper.findAll();
    PageInfo<User> pageInfo = new PageInfo<>(list);
    List<User> list1 = pageInfo.getList();
    model.addAttribute("userList", list1);
    return "test02";
  }

  @RequestMapping("/t6")
  public String t6() {
    String str = "asdf";
    char ch = str.charAt(10);
    return "test05";
  }

  @ExceptionHandler(Exception.class)
  @ResponseBody
  public String doEx() {
    return "出错了";
  }
}
